package com.swsc.solarverse.schedule;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.swsc.solarverse.common.config.ResParams;
import com.swsc.solarverse.common.enums.OrganTypeEnums;
import com.swsc.solarverse.common.enums.StateEnums;
import com.swsc.solarverse.common.util.LocalDateTimeUtils;
import com.swsc.solarverse.component.redis.StringRedisService;
import com.swsc.solarverse.dao.entity.forward.ForwardStringLeDiagnoseRecordDO;
import com.swsc.solarverse.dao.entity.sys.CorporationDO;
import com.swsc.solarverse.service.forward.IForwardStringDataCurrentdayService;
import com.swsc.solarverse.service.forward.IForwardStringLeDiagnoseRecordService;
import com.swsc.solarverse.service.schedule.StringIuAnalysisScheduleTaskService;
import com.swsc.solarverse.service.schedule.StringIuAnalysisService;
import com.swsc.solarverse.service.sys.ICorporationService;
import org.apache.commons.collections4.CollectionUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author chencl
 * @Date 2024/5/7 15:12
 * @Version 1.0
 * @Description 组串IU分析
 */
@lombok.extern.slf4j.Slf4j
@org.springframework.stereotype.Component
public class StringIuAnalysisScheduleTask {

    private final ICorporationService corporationService;
    private final IForwardStringDataCurrentdayService forwardStringDataCurrentdayService;
    private final StringRedisService stringRedisService;
    private final IForwardStringLeDiagnoseRecordService forwardStringLeDiagnoseRecordService;
    private final StringIuAnalysisScheduleTaskService stringIuAnalysisScheduleTaskService;
    private final StringIuAnalysisService stringIuAnalysisService;

    public StringIuAnalysisScheduleTask(ICorporationService corporationService,
                                        IForwardStringDataCurrentdayService forwardStringDataCurrentdayService,
                                        StringRedisService stringRedisService,
                                        IForwardStringLeDiagnoseRecordService forwardStringLeDiagnoseRecordService,
                                        StringIuAnalysisScheduleTaskService stringIuAnalysisScheduleTaskService,
                                        StringIuAnalysisService stringIuAnalysisService) {
        this.corporationService = corporationService;
        this.forwardStringDataCurrentdayService = forwardStringDataCurrentdayService;
        this.stringRedisService = stringRedisService;
        this.forwardStringLeDiagnoseRecordService = forwardStringLeDiagnoseRecordService;
        this.stringIuAnalysisScheduleTaskService = stringIuAnalysisScheduleTaskService;
        this.stringIuAnalysisService = stringIuAnalysisService;
    }

    @lombok.Data
    public static class StringIuDataAnalysisVO {
        private Integer tenantNo;
        private Integer organNo;
        private Long devicePid;
        private Long deviceId;
        private String deviceName;
        private String deviceCode;

        private Double avgI;
        private Double avgU;
        private Double submatrixAvgI;
        private Double submatrixAvgU;

        public StringIuDataAnalysisVO(Integer tenantNo, Integer organNo, Long devicePid, Long deviceId, String deviceName, String deviceCode) {
            this.tenantNo = tenantNo;
            this.organNo = organNo;
            this.devicePid = devicePid;
            this.deviceId = deviceId;
            this.deviceName = deviceName;
            this.deviceCode = deviceCode;
        }

        public StringIuDataAnalysisVO() {
        }
    }

    @org.springframework.scheduling.annotation.Async
    @org.springframework.scheduling.annotation.Scheduled(cron = "0 5 5 * * ?")
    public void backupRecord() {
        if (!ResParams.analysisStrIuData) {
            log.info("未开启组串IU分析");
            return;
        }
        log.info("低效诊断-备份数据开始...");
        log.info("低效诊断-删除90天以前的历史诊断数据");
        LocalDateTime now = LocalDateTime.now();
        String diagnoseTimeStr = LocalDateTimeUtils.time2Str(now.minusDays(366L), "yyyy-MM-dd HH:mm:ss");
        now = LocalDateTimeUtils.getDayStart(now);
        forwardStringLeDiagnoseRecordService.deleteHisByDataTime(diagnoseTimeStr);
        log.info("低效诊断-备份昨天的诊断数据");
        forwardStringLeDiagnoseRecordService.backupDiagnoseRecordByTimeRange(LocalDateTimeUtils.getDayStart(now.minusDays(1L)), LocalDateTimeUtils.getDayEnd(now.minusDays(1L)));
        log.info("低效诊断-清空30天前的诊断数据");
        forwardStringLeDiagnoseRecordService.remove(Wrappers.lambdaQuery(ForwardStringLeDiagnoseRecordDO.class)
                .lt(ForwardStringLeDiagnoseRecordDO::getDiagnoseTime, now.minusDays(180L)));
        log.info("低效诊断-备份数据结束...");

    }

    /**
     * 每天21点计算今天的
     */
    @org.springframework.scheduling.annotation.Async
    @org.springframework.scheduling.annotation.Scheduled(cron = "0 0 21 * * ?")
    public void analysisNew() {
        if (!ResParams.analysisStrIuData) {
            log.info("组串IU低效诊断分析-未开启");
            return;
        }
        LocalDateTime now = LocalDateTime.now();
        List<CorporationDO> corporationDos = corporationService.list(Wrappers.lambdaQuery(CorporationDO.class)
                .eq(CorporationDO::getOrganType, OrganTypeEnums.PS.getType())
                .eq(CorporationDO::getState, StateEnums.ENABLE.getEnable()));
        if (CollectionUtils.isNotEmpty(corporationDos)) {
            for (CorporationDO corporationDo : corporationDos) {
                Integer tenantNo = corporationDo.getTenantNo();
                Integer organNo = corporationDo.getOrganNo();
                // 具体分析逻辑
                stringIuAnalysisService.analysisNew(tenantNo, organNo, now, now);
            }
        }

    }



}
